namespace Northwind.Web.App
{
    using System;
    using Northwind.Domain.Core;
    using Northwind.Domain.Core.Entities;
    using NRepository.Core.Command;

    public class TrackedEntitiesModifyCommandInterceptor : IModifyCommandInterceptor
    {
        public void Modify<T>(ICommandRepository repository, Action<T> modifyAction, T entity) where T : class
        {
            if (typeof(ITrackedEntityBase).IsAssignableFrom(typeof(T)))
            {
                var trackedEntity = (ITrackedEntityBase)entity;
                trackedEntity.ModifiedBy = CurrentPrincipalHelper.Name;
                trackedEntity.ModifiedOn = DateTime.Now;
            }

            modifyAction(entity);
        }
    }
}
